;
(function (call) {
    var main = {
        userInfo: null,
        warnUnit: {
            'S_RAIN10M': { unit: "mm", name: "10分钟降水" },
            'S_RAIN1': { unit: "mm", name: "1小时降水" },
            'S_RAIN3': { unit: "mm", name: "3小时降水" },
            'S_GALE': { unit: "m/s", name: "10m风" },
            'S_GUST': { unit: "m/s", name: "阵风" },
            'S_V': { unit: "m", name: "能见度" },
            'S_H_T': { unit: "℃", name: "高温" },
        },
        sKElementId: [
            { "name": "气温", "elementId": "T",  "unit": '℃', "color": '#fe5150' },
            { "name": "降水", "elementId": "RAIN", "unit": 'mm', "color": '#00d4bb' },
            { "name": "风", "elementId": "WS_10M,WD_10M", "unit": 'm/s', "color": '#266fef' }, //风速，风向
            { "name": "能见度", "elementId": "VIS_10M", "unit": 'km', "color": '#ffa544' }
        ],
        yBElementId: [
            { "name": "气温", "elementId": "TMP", "unit": '℃', "color": '#fe5150' },
            { "name": "降水", "elementId": "TP1H", "unit": 'mm', "color": '#00d4bb' },
            { "name": "风", "elementId": "EDA", "unit": 'm/s', "color": '#266fef' }, //风速，风向
            { "name": "能见度", "elementId": "VIS", "unit": 'km', "color": '#ffa544' }
        ],
        echartOptionObj: {},
        echartObj: null,
        init: function () {
            this.initEchartOption();
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            this.initView();
            this.getXAxioData();
            this.getData();
        },
        getXAxioData: function () {
            var skTime = window.dataAnalysisObj.skTime;
            var date = new Date(new Date(skTime).setHours(new Date(skTime).getHours() - 11));
            var xAxioArr = [];
            for (var i = 0; i < 24; i++) {
                xAxioArr.push(date.format("yyyy-MM-dd hh:00:00"));
                date.setHours(date.getHours() + 1);
            }
            this.echartOptionObj.xAxis.categories = xAxioArr;
        },
        initView: function () {
            var stationInfo = window.dataAnalysisObj.stationInfo;
            $(".dataAnalysis_header .stationName").text(stationInfo.stationName);
            $(".dataAnalysis_header .stationId").text(stationInfo.stationId);
            $(".dataAnalysis_header .stationLon").text(stationInfo.lon);
            $(".dataAnalysis_header .stationLat").text(stationInfo.lat);
            var name = this.warnUnit[stationInfo.warnName].name + "：";
            var val = this.getUnitByWarnId(stationInfo);
            $(".dataAnalysis_header >div").first().append("<div>" + name + "<span>" + val + "</span></div>");
        },
        getUnitByWarnId: function (stationInfo) {
            var that = this;
            var msg = stationInfo.val + that.warnUnit[stationInfo.warnName].unit;
            if (stationInfo.warnName === "S_GALE") {
                var windx = WindUtil.getWindDire(window.WindUtil.calcWindDire(stationInfo.val2));
                msg = msg + " " + windx.CH;
            }
            return msg;
        },
        getData() {
            var that = this;
            var skParam = this.getSkData();
            var ybParam = this.getYbData();
            $.when($.getJSON(api.internal.business.getSkEchartData, skParam)).done(function (skData) {
                $.getJSON(api.internal.business.getGdybDataByParamData, ybParam, function (ybData) {
                    debugger
                    if (!skData.isSuccess || !ybData.success) {
                        return false;
                    }
                    var key = window.dataAnalysisObj.stationInfo.lon + "_" + window.dataAnalysisObj.stationInfo.lat
                    that.setchartData(skData.data, ybData.resultData[key]);
                })
            })
        },
        setchartData: function (skData, ybData) {
            var that = this;
            var opposite = false;
            if (skData.length > 0) {
                skData = skData.filter(function(data){
                    return data.time.slice(14, 16) == '00';
                })
            }
            
            that.sKElementId.forEach(function (ele, index) {
                var seriesObj = that.getSeries();
                seriesObj.name = ele.name;
                seriesObj["yAxis"] = index;
                that.echartOptionObj.colors.push(ele.color);
                if (ele.elementId.indexOf("RAIN") > -1) {
                    seriesObj["type"] = "column";
                } else {
                    seriesObj["type"] = "spline";
                }
                for (var i = 0; i < 12; i++) {
                    var val = skData[i][ele.elementId.split(",")[0]];
                    val = val == null ? val : Number(Number(val).toFixed(1));
                    if (ele.elementId.indexOf("WS_") > -1) {
                        var windD = skData[i][ele.elementId.split(",")[1]];
                        windD = windD == null ? windD : Number(windD);
                        var windx = window.WindUtil.getWindDire(window.WindUtil.calcWindDire(windD));
                        var serieDataItem = {
                            y: val,
                            unit: ele.unit,
                            wd: windx.CH
                        };
                        serieDataItem.marker = {
                            symbol: "url(/business/common/css/images/wind/" + windx.EN + ".png)"
                        }
                        seriesObj.data.push(serieDataItem);
                    } else {
                        if (ele.elementId.indexOf("VIS_10M") > -1 && val != null) {
                            val = val / 1000 > 100 ? null : val / 1000;
                        }
                        seriesObj.data.push({
                            y: val,
                            unit: ele.unit
                        });
                    }
                }
                var yAxisObj = that.getYAxis();
                yAxisObj.title.text = ele.unit;
                opposite = !opposite;
                if (opposite) {
                    yAxisObj["opposite"] = true;
                    yAxisObj.title.x = 10
                } else {
                    yAxisObj.title.x = -10
                }
                if(ele.elementId.indexOf("RAIN") > -1){
                    yAxisObj.labels.formatter = function() {
                        return Highcharts.numberFormat(this.value, 1);
                    };
                }
                yAxisObj.title.style.color = ele.color;
                yAxisObj.labels.style.color = ele.color;
                that.echartOptionObj.series.push(seriesObj);
                that.echartOptionObj.yAxis.push(yAxisObj);
            })
            that.yBElementId.forEach(function (ele, index) {
                if (ele.elementId != "EDA") {
                    for (var i = 0; i < 12; i++) {
                        var val = ybData[ele.elementId][0][i];
                        val = val == 9999 ? null : val;
                        that.echartOptionObj.series[index].data.push({
                            y: val,
                            unit: ele.unit
                        });
                    }
                } else {
                    for (var i = 0; i < 12; i++) {
                        var windS = ybData[ele.elementId][1][i];
                        var windD = ybData[ele.elementId][2][i];
                        var serieDataItem = {
                            y: windS,
                            unit: ele.unit,
                            wd: windD
                        };
                        var png = window.WindUtil.getWindPng[windD];
                        serieDataItem.marker = {
                            symbol: "url(/business/common/css/images/wind/" + png + ".png)"
                        }
                        that.echartOptionObj.series[index].data.push(serieDataItem);
                    }
                }
            })
            that.echartObj = new Highcharts.Chart(that.echartOptionObj);
        },
        getSkData: function () {
            var skTime = window.dataAnalysisObj.skTime;
            var endTime = new Date(skTime).format("yyyy-MM-dd hh:00:00");
            var startTime = new Date(new Date(skTime).setHours(new Date(skTime).getHours() - 11)).format("yyyy-MM-dd hh:00:00");;
            var param = {
                stationId: window.dataAnalysisObj.stationInfo.stationId,
                mins: 10,
                startTime: startTime,
                endTime: endTime,
                // stationId: 54523,
                // startTime: "2019-11-09 20:00:00",
                // endTime: "2019-11-10 08:00:00"
            }
            return param;
        },
        getYbData: function () {
            var that = this;
            var skTime = window.dataAnalysisObj.skTime;
            var periodObj = that.getYbStartPeriod(skTime);
            var stationInfo = window.dataAnalysisObj.stationInfo;
            var params = {
                fcstTime: periodObj.fcstTime, // 起报时间
                element: "TMP,TP1H,EDA,VIS",
                lon: stationInfo.lon,
                lat: stationInfo.lat,
                mode: 'GDMODE',
                startPeriod: periodObj.startPeriod,
                endPeriod: periodObj.startPeriod + 12, // 结束时效
                interval: 1, // 预报间隔
                count: 0,
                stationType: 3
            }
            return params;
        },
        getYbStartPeriod: function (skTime) {
            var hour = new Date(skTime).getHours();
            var date = new Date(skTime);
            var fcstTime = "";
            var startPeriod = 0;
            date.setDate(date.getDate() - 1);
            fcstTime = date.format('yyyyMMdd200000');
            startPeriod = hour + 24 - 20 + 1;
            return {
                fcstTime: fcstTime,
                startPeriod: startPeriod
            }
        },
        getHoverShouMag: function (index) {
            var that = this;
            var msg = "";
            Highcharts.each(that.echartObj.series, function (series) {
                debugger
                if (!series.isDirty) {
                    Highcharts.each(series.points, function (point, i) {
                        if (i === index) {
                            var val = point.y;
                            val = val == null ? '-' : val;
                            if (point.unit == "m/s") {
                                msg = msg + "<br/>" + point.series.name + "：" + val + point.unit + " " + point.wd;
                            } else {
                                msg = msg + "<br/>" + point.series.name + "：" + val + point.unit
                            }
                        }
                    });
                }

            });
            return msg;
        },
        initEchartOption: function () {
            var that = this;
            that.echartOptionObj = {
                chart: {
                    renderTo: "dataEchart",
                    backgroundColor: "rgba(0,0,0,0)",
                    marginLeft: 65,
                    marginRight: 75,
                    marginTop: 55
                },
                plotOptions: {
                    spline: {
                        marker: {
                            symbol: 'circle'
                        }
                    }
                },
                tooltip: {
                    style: {
                        fontSize: '14px'
                    },
                    formatter: function () {
                        return this.key + that.getHoverShouMag(this.point.index)
                    }
                },
                title: {
                    text: null
                },
                credits: {
                    enabled: false
                },
                exporting: {
                    enabled: false
                },
                colors: [],
                xAxis: {
                    categories: [],
                    labels: {
			        autoRotation: false,
                        style: {
                            fontSize: '13px'
                        },
                        formatter: function () {
                            var date = new Date(this.value);
                            var day = date.getDate();
                            var hour = date.getHours();
                            hour = hour > 9 ? hour : '0' + hour;
                            if (hour === "00") {
                                return hour + "<br>" + day + "日";
                            } else {
                                return hour
                            }
                        }
                    },
                    plotLines: [{
                        value: 11,
                        width: 1,
                        color: 'red',
                        zIndex: 999,
                        dashStyle: "dashdot"
                    }]
                },
                legend: {
                    align: "right",
                    verticalAlign: "top",
                    y: -10,
                    itemStyle: {
                        fontSize: '14px'
                    }
                },
                yAxis: [],
                series: []
            }
        },
        getSeries: function () {
            return {
                name: "",
                data: [],
                dataLabels: {
                    enabled: false
                }
            }
        },
        getYAxis: function () {
            return {
                title: {
                    align: 'high',
                    offset: -10,
                    rotation: 0,
                    x: 0,
                    y: -15,
                    style: {
                        fontSize: '13px'
                    }
                },
                labels: {
                    align: 'center',
                    style: {
                        fontSize: '13px'
                    }
                }
            }
        }
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
});